package com.example.config.appscheduledtasks;

import com.example.config.RedisUtil;
import com.example.service.appservice.AppForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

/**
 * @BelongsProject："smart-property"
 * @BelongsPackage："com.example.config"
 * @CreateTime："2022-11-17 15:51"
 * @Description：前端-定时更新数据库浏览量
 * @Author：LK
 */
@Component
@EnableScheduling
public class PostsFabulousAndBrowse {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AppForumService appForumService;

    /**
     * @description:把redis的数据定时更新到数据库-5分钟一次
     * @author: lk
     * @date: 2022/11/17 16:08
     **/
    @Bean
    @Scheduled(cron = "0 0/5 * * * ?")
    public void appBrowseCount(){
        System.out.println("持久化任务开启：");
        // 取出集合里所有的key和value，Map类型
        Map<Object, Object> liulanliang = redisUtil.hmget("liulanliang");
        // 遍历map
        liulanliang.forEach((o,o2) -> {
            // 持久化到数据库
            String a = (String) o;
            Integer b = (Integer) o2;
            appForumService.appBrowseCount(a,b);
        });
        // 把这个key清空
        redisUtil.del("liulanliang");
    }
}
